<?php

/**
 *
 * 关联操作挂件
 * @version $Id:$
 *
 */

namespace Todo\Widget;
use Think\Controller;
use Todo\Service\DictService;

class RelationWidget extends Controller{

	/**
	 * 生成单选框
	 * @param string $model 模型:Todo/Sys/Role
	 * @param string $name 单选框名称
	 * @param mixed $value 默认选中的值
	 * @param array|mixed $where=null 查询参数
	 * @param array $map=array('key'=>'id','title'=>'title') 字段
	 */
	public function radio($model, $name, $value, $where=null, $map = array('key'=>'id','title'=>'title')){
		$list = D($model)->where($where)->select();
		$this->assign('name', $name);
		$this->assign('value', $value);
		$this->assign('list',$list);
		$this->assign('map', $map);
		$this->display('./Common/View/Widget/relation_radio.html');
	}

	/**
	 * 生成多选框
	 * @param string $model 模型:Todo/Sys/Role
	 * @param string $name 单选框名称
	 * @param mixed $value 默认选中的值
	 * @param array|mixed $where=null 查询参数
	 * @param array $map=array('key'=>'id','title'=>'title') 字段
	 */
	public function checkbox($model, $name, $value, $where=null, $map = array('key'=>'id','title'=>'title')){
		$list = D($model)->where($where)->select();
		$this->assign('name', $name);
		if(is_array($value)){
			$this->assign('value', implode(',', $value));
			$this->assign('valArr', $value);
		}else{
			$this->assign('value', $value);
			$this->assign('valArr', explode(',', $value));
		}
		$this->assign('list',$list);
		$this->assign('map', $map);
		$this->display('./Common/View/Widget/relation_checkbox.html');
	}

	/**
	 * 生成下拉框
	 * @param string $model 模型:Todo/Sys/Role
	 * @param mixed $value 默认选中的值
	 * @param array|mixed $where=null 查询参数
	 * @param array $map=array('key'=>'id','title'=>'title') 字段
	 */
	public function select($model, $value, $where=null, $map = array('key'=>'id','title'=>'title')){
		$list = D($model)->where($where)->select();
		$this->assign('value', $value);
		$this->assign('list',$list);
		$this->assign('map', $map);
		$this->display('./Common/View/Widget/relation_checkbox.html');
	}
}